﻿using System;
using System.Windows.Forms;
using Pda_Total_Station;
using System.Threading;
using System.Collections.Generic;
using System.IO;

namespace DistMeasure
{
    public partial class 测距仪管理 : Form
    {
        DistantMeasureCom geo;
        public 测距仪管理()
        {
            InitializeComponent();
        }




        #region 测距
        MeasureDistanceData data;
        private void beginbtn_Click(object sender, EventArgs e)
        {
            this.beginbtn.Enabled = false;
            this.txtsend.Text = "时间\t编号\t距离\r\n";
            data = geo.Measure(Convert.ToInt32(textBox1.Text));
            string temp = "";
            this.Invoke((ThreadStart)delegate
            {
                temp = string.Format("{0}\t{1}\t{2}\r\n", data.Time.ToString("HH:mm:ss"), data.Id, data.Distance);
                this.txtsend.Text = txtsend.Text + temp;
                this.txtsend.Select(this.txtsend.Text.Length - 1, 0);
                this.txtsend.ScrollToCaret();
                this.beginbtn.Enabled = true;

            });
            if (data.Distance > 0)
            {
                //save to file
                string path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase.ToString())
                + "\\测距仪_" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                using (StreamWriter sw = new StreamWriter(path, true))
                {
                    sw.Write(temp);
                }
            }
            //try
            //{
    
            //}
            //catch (Exception a)
            //{
            //    MessageBox.Show(a.Message);
            //    return;
            //}

        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text == "打开")
            {
                try
                {
                    geo = new DistantMeasureCom(comboBox1.Text);
                    button1.Text = "关闭";
                }
                catch
                {
                    return;
                }
            }
            else
            {
                geo.ClosePort();
                button1.Text = "打开";

            }
        }
        #endregion

        private void btn_SuveyAll_Click(object sender, EventArgs e)
        {
            this.txtsend.Text = "时间\t编号\t距离\r\n";
            int MinPtId = Convert.ToInt32(minPtIdTxt.Text);
            int MaxPtId = Convert.ToInt32(maxPtIdTxt.Text);
            MeasureDistanceData[] array = new MeasureDistanceData[MaxPtId - MinPtId + 1];
            List<int> UnSuccessIdList = new List<int>();//补测id列表
            for (int i = MinPtId; i <= MaxPtId; i++)
            {
                try
                {
                    MeasureDistanceData d = geo.Measure(i);
                    if (d.Distance != 0)
                    {
                        array[i - MinPtId] = d;
                    }
                    else
                    {
                        UnSuccessIdList.Add(i);
                    }
                    this.txtsend.Invoke((ThreadStart)delegate
                    {
                        string temp = string.Format("{0}\t{1}\t{2}\r\n", d.Time.ToString("HH:mm:ss"), d.Id, d.Distance);
                        this.txtsend.Text = txtsend.Text + temp;
                        this.txtsend.Select(this.txtsend.Text.Length - 1, 0);
                        this.txtsend.ScrollToCaret();
                    });
                }
                catch
                {
                    continue;
                }

            }
            Thread.Sleep(200);
            if (UnSuccessIdList.Count > 0)
            {
                this.txtsend.Invoke((ThreadStart)delegate
                {
                    string temp = "-----------重测---------\r\n";
                    this.txtsend.Text = txtsend.Text + temp;
                    this.txtsend.Select(this.txtsend.Text.Length - 1, 0);
                    this.txtsend.ScrollToCaret();
                });
            }
            Thread.Sleep(200);
            //补测法则
            for (int k = 0; k < UnSuccessIdList.Count; k++)
            {
                try
                {
                    int unSuccessId = UnSuccessIdList[k];
                    MeasureDistanceData d = geo.Measure(unSuccessId);
                    array[unSuccessId - MinPtId] = d;
                    this.txtsend.Invoke((ThreadStart)delegate
                    {
                        string temp = string.Format("{0}\t{1}\t{2}\r\n", d.Time.ToString("HH:mm:ss"), d.Id, d.Distance);
                        this.txtsend.Text = txtsend.Text + temp;
                        this.txtsend.Select(this.txtsend.Text.Length - 1, 0);
                        this.txtsend.ScrollToCaret();
                    });
                }
                catch
                {
                    continue;
                }
            }
            this.txtsend.Invoke((ThreadStart)delegate
            {
                string temp = "-----------测量完成---------\r\n";
                this.txtsend.Text = txtsend.Text + temp;
                this.txtsend.Select(this.txtsend.Text.Length - 1, 0);
                this.txtsend.ScrollToCaret();
            });

            //save to file
            string path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase.ToString())
            + "\\测距仪_" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
            using (StreamWriter sw = new StreamWriter(path, true))
            {
                sw.Write(this.txtsend.Text);
            }

        }



    }
}